package cn.codeforfun.user;

import cn.codeforfun.entity.RequestParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by Administrator on 2016/12/11 0011.
 */
@Service
public class UserService {
    @Autowired
    private UserDao userDao;

    public Page<User> pageList(RequestParam requestParam) {
        String param = requestParam != null ? requestParam.getParam() : null;
        Pageable pageable = new PageRequest(requestParam.getPage() - 1, requestParam.getSize());
        if (StringUtils.isNotEmpty(param)) {
            return userDao.findByNameLikeOrCodeLikeOrPhoneLike(param, param, param, pageable);
        } else {
            return userDao.findAll(pageable);
        }
    }

    public void save(User user) {
        if (StringUtils.isEmpty(user.getCode())) {
            user.setCode("S" + new Date().getTime());
        }
        userDao.save(user);
    }
}
